home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / JToggleButton.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  10.6 KB  |  358 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)JToggleButton.java    1.37 98/09/04
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18.  
  19. import javax.swing.event.*;
  20. import javax.swing.plaf.*;
  21. import javax.accessibility.*;
  22.  
  23. import java.io.ObjectOutputStream;
  24. import java.io.ObjectInputStream;
  25. import java.io.IOException;
  26.  
  27.  
  28. /**
  29.  * An implementation of a two-state button.  
  30.  * The <code>JRadioButton</code> and <code>JCheckBox</code> classes
  31.  * are subclasses of this class.
  32.  * <p>
  33.  * For the keyboard keys used by this component in the standard Look and
  34.  * Feel (L&F) renditions, see the
  35.  * <a href="doc-files/Key-Index.html#JToggleButton">JToggleButton</a> key assignments.
  36.  * <p>
  37.  * <strong>Warning:</strong>
  38.  * Serialized objects of this class will not be compatible with
  39.  * future Swing releases.  The current serialization support is appropriate
  40.  * for short term storage or RMI between applications running the same
  41.  * version of Swing.  A future release of Swing will provide support for
  42.  * long term persistence.
  43.  *
  44.  * @beaninfo
  45.  *   attribute: isContainer false
  46.  * 
  47.  * @see JRadioButton
  48.  * @see JCheckBox
  49.  * @version 1.37 09/04/98
  50.  * @author Jeff Dinkins
  51.  */
  52. public class JToggleButton extends AbstractButton implements Accessible {
  53.  
  54.     /**
  55.      * @see #getUIClassID
  56.      * @see #readObject
  57.      */
  58.     private static final String uiClassID = "ToggleButtonUI";
  59.  
  60.     /**
  61.      * Creates an initially unselected toggle button
  62.      * without setting the text or image.
  63.      */
  64.     public JToggleButton () {
  65.         this(null, null, false);
  66.     }
  67.  
  68.     /**
  69.      * Creates an initially unselected toggle button
  70.      * with the specified image but no text.
  71.      *
  72.      * @param icon  the image that the button should display
  73.      */
  74.     public JToggleButton(Icon icon) {
  75.         this(null, icon, false);
  76.     }
  77.     
  78.     /**
  79.      * Creates a toggle button with the specified image 
  80.      * and selection state, but no text.
  81.      *
  82.      * @param icon  the image that the button should display
  83.      * @param selected  if true, the button is initially selected;
  84.      *                  otherwise, the button is initially unselected
  85.      */
  86.     public JToggleButton(Icon icon, boolean selected) {
  87.         this(null, icon, selected);
  88.     }
  89.     
  90.     /**
  91.      * Creates an unselected toggle button with the specified text.
  92.      *
  93.      * @param text  the string displayed on the toggle button
  94.      */
  95.     public JToggleButton (String text) {
  96.         this(text, null, false);
  97.     }
  98.  
  99.     /**
  100.      * Creates a toggle button with the specified text
  101.      * and selection state.
  102.      *
  103.      * @param text  the string displayed on the toggle button
  104.      * @param selected  if true, the button is initially selected;
  105.      *                  otherwise, the button is initially unselected
  106.      */
  107.     public JToggleButton (String text, boolean selected) {
  108.         this(text, null, selected);
  109.     }
  110.  
  111.     /**
  112.      * Creates a toggle button that has the specified text and image,
  113.      * and that is initially unselected.
  114.      *
  115.      * @param text the string displayed on the button
  116.      * @param icon  the image that the button should display
  117.      */
  118.     public JToggleButton(String text, Icon icon) {
  119.         this(text, icon, false);
  120.     }
  121.  
  122.     /**
  123.      * Creates a toggle button with the specified text, image, and
  124.      * selection state.
  125.      *
  126.      * @param text the text of the toggle button.
  127.      * @param selected  if true, the button is initially selected;
  128.      *                  otherwise, the button is initially unselected
  129.      */
  130.     public JToggleButton (String text, Icon icon, boolean selected) {
  131.         // Create the model
  132.         setModel(new ToggleButtonModel());
  133.  
  134.         model.setSelected(selected);
  135.  
  136.         // initialize
  137.         init(text, icon);
  138.     }
  139.  
  140.     /**
  141.      * Notification from the UIFactory that the L&F
  142.      * has changed. 
  143.      *
  144.      * @see JComponent#updateUI
  145.      */
  146.     public void updateUI() {
  147.         setUI((ButtonUI)UIManager.getUI(this));
  148.     }
  149.     
  150.     /**
  151.      * Returns a string that specifies the name of the l&f class
  152.      * that renders this component.
  153.      *
  154.      * @return String "ToggleButtonUI"
  155.      * @see JComponent#getUIClassID
  156.      * @see UIDefaults#getUI
  157.      * @beaninfo
  158.      *  description: A string that specifies the name of the L&F class
  159.      */
  160.     public String getUIClassID() {
  161.         return uiClassID;
  162.     }
  163.  
  164.  
  165.     // *********************************************************************
  166.  
  167.     /**
  168.      * The ToggleButton model
  169.      * <p>
  170.      * <strong>Warning:</strong>
  171.      * Serialized objects of this class will not be compatible with
  172.      * future Swing releases.  The current serialization support is appropriate
  173.      * for short term storage or RMI between applications running the same
  174.      * version of Swing.  A future release of Swing will provide support for
  175.      * long term persistence.
  176.      */
  177.     public static class ToggleButtonModel extends DefaultButtonModel {
  178.  
  179.         /**
  180.          * Creates a new ToggleButton Model
  181.          */
  182.         public ToggleButtonModel () {
  183.         }
  184.  
  185.         /**
  186.          * Checks if the button is selected.
  187.          */
  188.         public boolean isSelected() {
  189.             if(group != null) {
  190.                 return group.isSelected(this);
  191.             } else {
  192.                 return (stateMask & SELECTED) != 0;
  193.             }
  194.         }
  195.  
  196.  
  197.         /**
  198.          * Sets the selected state of the button.
  199.          * @param b true selects the toggle button,
  200.          *          false deselects the toggle button.
  201.          */
  202.         public void setSelected(boolean b) {
  203.             // if (this.isSelected() == b) {
  204.             // return;
  205.             // }
  206.                 
  207.             if(group != null) {
  208.                 // use the group model instead
  209.                 group.setSelected(this, b);
  210.             } else {
  211.                 if (b) {
  212.                     stateMask |= SELECTED;
  213.                 } else {
  214.                     stateMask &= ~SELECTED;
  215.                 }
  216.             }
  217.  
  218.             // Send ChangeEvent
  219.             fireStateChanged();
  220.  
  221.             // Send ItemEvent
  222.             fireItemStateChanged(
  223.                     new ItemEvent(this,
  224.                                   ItemEvent.ITEM_STATE_CHANGED,
  225.                                   this,
  226.                                   this.isSelected() ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
  227.         
  228.         }
  229.  
  230.         /**
  231.          * Sets the pressed state of the toggle button.
  232.          */ 
  233.         public void setPressed(boolean b) {
  234.             if ((isPressed() == b) || !isEnabled()) {
  235.                 return;
  236.             }
  237.  
  238.             if (b == false && isArmed()) {
  239.                 setSelected(!this.isSelected());
  240.             } 
  241.  
  242.             if (b) {
  243.                 stateMask |= PRESSED;
  244.             } else {
  245.                 stateMask &= ~PRESSED;
  246.             }
  247.  
  248.             fireStateChanged();
  249.  
  250.             if(!isPressed() && isArmed()) {
  251.                 fireActionPerformed(
  252.                     new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  253.                                     getActionCommand())
  254.                     );
  255.             }
  256.  
  257.         }
  258.     }
  259.  
  260.  
  261.     /** 
  262.      * See readObject() and writeObject() in JComponent for more 
  263.      * information about serialization in Swing.
  264.      */
  265.     private void writeObject(ObjectOutputStream s) throws IOException {
  266.         s.defaultWriteObject();
  267.     if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  268.         ui.installUI(this);
  269.     }
  270.     }
  271.  
  272.  
  273.     /**
  274.      * Returns a string representation of this JToggleButton. This method 
  275.      * is intended to be used only for debugging purposes, and the 
  276.      * content and format of the returned string may vary between      
  277.      * implementations. The returned string may be empty but may not 
  278.      * be <code>null</code>.
  279.      * <P>
  280.      * Overriding paramString() to provide information about the
  281.      * specific new aspects of the JFC components.
  282.      * 
  283.      * @return  a string representation of this JToggleButton.
  284.      */
  285.     protected String paramString() {
  286.         return super.paramString();
  287.     }
  288.  
  289.  
  290. /////////////////
  291. // Accessibility support
  292. ////////////////
  293.  
  294.     /**
  295.      * Get the AccessibleContext associated with this JComponent
  296.      *
  297.      * @return the AccessibleContext of this JComponent
  298.      * @beaninfo
  299.      *       expert: true
  300.      *  description: The AccessibleContext associated with this ToggleButton.
  301.      */
  302.     public AccessibleContext getAccessibleContext() {
  303.         if (accessibleContext == null) {
  304.             accessibleContext = new AccessibleJToggleButton();
  305.         }
  306.         return accessibleContext;
  307.     }
  308.  
  309.     /**
  310.      * The class used to obtain the accessible role for this object.
  311.      * <p>
  312.      * <strong>Warning:</strong>
  313.      * Serialized objects of this class will not be compatible with
  314.      * future Swing releases.  The current serialization support is appropriate
  315.      * for short term storage or RMI between applications running the same
  316.      * version of Swing.  A future release of Swing will provide support for
  317.      * long term persistence.
  318.      */
  319.     protected class AccessibleJToggleButton extends AccessibleAbstractButton 
  320.         implements ItemListener {
  321.  
  322.     public AccessibleJToggleButton() {
  323.         super();
  324.         JToggleButton.this.addItemListener(this);
  325.     }
  326.  
  327.     /**
  328.      * Fire accessible property change events when the state of the
  329.      * toggle button changes.
  330.      */
  331.         public void itemStateChanged(ItemEvent e) {
  332.             JToggleButton tb = (JToggleButton) e.getSource();
  333.             if (JToggleButton.this.accessibleContext != null) {
  334.                 if (tb.isSelected()) {
  335.                     JToggleButton.this.accessibleContext.firePropertyChange(
  336.                             AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  337.                             null, AccessibleState.CHECKED);
  338.                 } else {
  339.                     JToggleButton.this.accessibleContext.firePropertyChange(
  340.                             AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
  341.                             AccessibleState.CHECKED, null);
  342.                 }
  343.             }
  344.         }
  345.  
  346.         /**
  347.          * Get the role of this object.
  348.          *
  349.          * @return an instance of AccessibleRole describing the role of the 
  350.          * object
  351.          */
  352.         public AccessibleRole getAccessibleRole() {
  353.             return AccessibleRole.TOGGLE_BUTTON;
  354.         }
  355.     } // inner class AccessibleJToggleButton
  356. }
  357.   
  358.